# encoding: utf-8
# @File  : node.py
# @Author: shaoyun
# @Date  :  2025/05/11
from sqlalchemy import Column, String, Float, DateTime, Enum, Boolean, Integer
from sqlalchemy.orm import relationship
from app.db.mysql import Base
from datetime import datetime
import enum

class NodeStatus(enum.Enum):
    """节点状态枚举"""
    ONLINE = "online"
    OFFLINE = "offline"
    DISABLED = "disabled"

class Node(Base):
    """节点模型"""
    __tablename__ = "nodes"

    id = Column(String(36), primary_key=True, index=True)
    name = Column(String(255), nullable=False)
    host_ip = Column(String(50), nullable=False)  # 新增：主机 IP
    ip = Column(String(50), nullable=False)
    port = Column(Integer, nullable=False)  # 改为 Integer
    container_id = Column(String(64), nullable=True)  # 新增：Docker 容器 ID
    image_name = Column(String(100), nullable=True)  # 新增：镜像名称
    status = Column(Enum(NodeStatus), default=NodeStatus.OFFLINE, index=True)
    cpu_usage = Column(Float, default=0.0)
    memory_usage = Column(Float, default=0.0)
    disk_usage = Column(Float, nullable=True)  # 改为 Float
    is_master = Column(Boolean, default=False)
    description = Column(String(500), nullable=True)
    last_heartbeat = Column(DateTime, default=datetime.utcnow, nullable=False)  # 改为不可空
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    tasks = relationship("TaskNodeAssociation", back_populates="node")

    def __repr__(self):
        return f"<Node {self.id}: {self.name}>"